import request from '@/utils/request'
import type { LoginParams, LoginResponse, User, PageParams, PageResponse } from './types'

// 用户登录
export const login = (data: LoginParams) => {
  return request<LoginResponse>({
    url: '/login',
    method: 'post',
    data
  })
}

// 获取图形验证码
export const getImageCaptcha = () => {
  return request<{
    id: string
    captcha: string
  }>({
    url: '/captcha/image',
    method: 'post'
  })
}

// 获取用户信息
export const getUserInfo = () => {
  return request<User>({
    url: '/secure/account/info',
    method: 'post'
  })
}

// 用户登出
export const logout = () => {
  return request({
    url: '/auth/logout',
    method: 'post'
  })
}

// 获取用户列表
export const getUserList = (params: PageParams & { keyword?: string }) => {
  return request<PageResponse<User>>({
    url: '/user/list',
    method: 'get',
    params
  })
}

// 创建用户
export const createUser = (data: Partial<User>) => {
  return request<User>({
    url: '/user',
    method: 'post',
    data
  })
}

// 更新用户
export const updateUser = (id: number, data: Partial<User>) => {
  return request<User>({
    url: `/user/${id}`,
    method: 'put',
    data
  })
}

// 删除用户
export const deleteUser = (id: number) => {
  return request({
    url: `/user/${id}`,
    method: 'delete'
  })
}

// 修改密码
export const changePassword = (data: { oldPassword: string; newPassword: string }) => {
  return request({
    url: '/user/change-password',
    method: 'post',
    data
  })
}